import util from '@/assets/js/util';
import axios from './axios'

export default {
  /**
   * user
   */
  // user登录
  login(data) {
    data.loading = true
    return axios.http('/User/UserLogin', 'post', data);
  },
  getMarkUsers(data) {
    return axios.http('/User/GetUsers', 'get', data);
  },
  setUser(data) {
		return axios.http('/User/SetUser', 'post', data)
	},
  /**
   * mark
   */
  getMarksDate(data) {
    return axios.http('/Mark/GetMarksDate', 'get', data);
  },
  getMarks(data) {
    data.loading = true
    return axios.http('/Mark/GetMarks', 'get', data);
  },
  getMarksById(data) {
    data.loading = true
    return axios.http('/Mark/GetMarksById', 'get', data);
  },
  createMark(data) {
    data.loading = true
		return axios.http('/Mark/CreateMark', 'post', data)
	},
	setMark(data) {
    data.loading = true
		return axios.http('/Mark/SetMark', 'post', data)
	},

  /**
   * public api
   */
  // axios调用外部api
  getPubApi(url) {
    return axios.http(url, 'get', {});
  },
  // josnp跨域调用外部api
  jsonpAction(url) {
    url=url+'&output=jsonp&callback=getJsonpData'
    //动态创建script加载数据
    const JSONP_SCRIPT = document.getElementById('JSONP_SCRIPT')
    if (JSONP_SCRIPT) document.body.removeChild(JSONP_SCRIPT)
    const jsonp = document.createElement("script")
    jsonp.setAttribute('id', 'JSONP_SCRIPT')
    jsonp.src = url
    document.body.appendChild(jsonp)
    //动态创建getJsonpData函数执行回调脚本
    try {
      if(typeof(eval(getJsonpData))=="function"){
        console.log('getJsonpData已存在')
      }
    } catch (error) {
      // console.log(error)
      const fun = document.createElement("script")
      fun.innerHTML = `
        function getJsonpData(r) {
          // console.log(r)
          localStorage.setItem('jsonpData',JSON.stringify(r))
        }
      `
      document.body.appendChild(fun)
    }
    // 加载script获取数据
    return new Promise((resolve, reject) => {
      jsonp.onload = function() {
        let d = localStorage.getItem('jsonpData')
        let r = null
        if (d && !util.isNull(d)) {
          r = JSON.parse(d)
          resolve(r)
        } else {
          reject(error)
        }
      }
    })
  }, 
}



